package org.jeecg.modules.flow.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.flow.entity.FlowAudit;
import org.jeecg.modules.flow.query.StartInstanceQuery;
import org.jeecg.modules.flow.query.SubmitTaskQuery;
import org.jeecg.modules.flow.service.IFlowBaseService;
import org.jeecg.modules.flow.vo.ProInstanceVo;
import org.jeecg.modules.flow.vo.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(tags = "基础流程API")
@RestController
@RequestMapping("/flow/base")
public class FlowBaseController {

    @Autowired
    private IFlowBaseService flowBaseService;

    /**
     * 启动流程实例
     * - 根据流程种类、发起人机构号，核心企业号。
     *
     * @param query 启动流程实例封装类
     * @return 返回启动后的实例ID
     */
    @PostMapping(path = "/start")
    @ApiOperation(value = "启动流程实例")
    public Result<String> startInstance(@RequestBody @Validated StartInstanceQuery query) {
        Result<String> result = flowBaseService.startInstance(query);
        return result;
    }


    /**
     * 根据流程实例ID查询流程实例
     *
     * @param instanceId
     * @return
     */

    @GetMapping("/searchInstanceById")
    @ApiOperation(value = "根据流程-实例ID-查询流程实例", notes = "查询流程实例")
    public Result<ProInstanceVo> searchInstanceById(@RequestParam String instanceId) {
        ProInstanceVo instance = flowBaseService.searchInstanceById(instanceId);
        return Result.OK(instance);
    }


    /**
     * 根据流程实例ids查询流程实例列表
     *
     * @param instanceIds
     * @return
     */
    @PostMapping("/searchInstanceByIds")
    @ApiOperation(value = "查询流程实例列表-根据流程实例ids", notes = "查询流程实例")
    public Result<List<ProInstanceVo>> searchInstanceById(@RequestBody Set<String> instanceIds) {
        List<ProInstanceVo> instances = flowBaseService.searchInstanceByIds(instanceIds);
        return Result.OK(instances);
    }

    /**
     * 根据流程名称模糊查询流程
     *
     * @param instanceName
     * @return
     */
    @GetMapping("/likeInstanceByName")
    @ApiOperation(value = "根据流程名称模糊查询流程", notes = "根据流程名称模糊查询流程id-名称列表")
    public Result<List<Map<String, Object>>> likeInstanceByName(@RequestParam String instanceName) {

        List<Map<String, Object>> result = flowBaseService.likeInstanceByName(instanceName);
        return Result.OK(result);
    }


    /**
     * 删除流程实例
     *
     * @param instanceId
     * @param reason     删除意见
     * @return
     */
    @PostMapping(path = "/delInstanceById")
    @ApiOperation(value = "根据流程-实例ID-删除流程实例", notes = "根据流程实例ID删除流程实例")
    public Result<Boolean> delInstanceById(@RequestParam String instanceId, @RequestParam String reason) {
        boolean isOk = flowBaseService.delInstanceById(instanceId, reason);
        if (!isOk) {
            return Result.error("删除失败", false);
        }
        return Result.OK(isOk);
    }

    @GetMapping(path = "/findTaskByAssignee")
    @ApiOperation(value = "查询代理人是当前用户的任务", notes = "根据用户ID查询任务列表")
    public Result<List<TaskVo>> findTaskByAssignee(@RequestParam("assignee") String assignee) {
        // 指定个人任务查询
        List<TaskVo> taskList = flowBaseService.findTaskByAssignee(assignee);
        return Result.OK(taskList);
    }


    /**
     * 根据流程实例查看流程变量
     *
     * @param taskId 任务ID,
     * @return 返回流程实例变量
     */
    @ApiOperation(value = "根据taskId查看流程变量", notes = "")
    @PostMapping("/getVariablesByTaskId")
    public Map<String, Object> getVariablesByTaskId(@RequestParam("taskId") String taskId,
                                                    @RequestBody List<String> variableNames,
                                                    @RequestParam Boolean isHistory) {
        Map<String, Object> variables = flowBaseService.getVariablesByTaskId(taskId, variableNames, isHistory);

        return variables;
    }


    @PostMapping(path = "/submitTask")
    @ApiOperation(value = "提交任务", notes = "提交任务（同意、退回、撤回、终结）")
    public Result<Boolean> submitTask(@RequestBody SubmitTaskQuery query) {
        return flowBaseService.submitTask(query);
    }

    @GetMapping(path = "/getInstanceByBusinessKey")
    @ApiOperation("根据业务BusinessKey获取流程实例信息")
    public ProInstanceVo getInstanceByBusinessKey(@RequestParam String businessKey) {
        ProInstanceVo proInstanceVo = flowBaseService.getInstanceByBusinessKey(businessKey);
        return proInstanceVo;
    }

    /**
     * 根据BusinessKey获取流程实实例下当前活动的任务
     *
     * @param businessKey
     * @return
     */
    @GetMapping(path = "/getActiveTaskByBusinessKey")
    @ApiOperation("根据业务BusinessKey获取流程实例下当前活动的任务信息")
    public TaskVo getActiveTaskByBusinessKey(@RequestParam String businessKey) {
        return flowBaseService.getActiveTaskByBusinessKey(businessKey);
    }


    @GetMapping(path = "/queryComment")
    @ApiOperation("获取任务审核信息")
    public Result<List<FlowAudit>> queryComment(@RequestParam String instanceId, String taskId) {
        List<FlowAudit> comment = flowBaseService.getComment(instanceId, taskId);
        return Result.OK(comment);
    }



    @GetMapping("/timeOut")
    @ApiOperation("超时测试")
    public Result<?> timeOut(@RequestParam Long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new JeecgBootException("超时了");
        }
        int i = 1 / 0;
        return Result.OK("ok", time);
    }

}
